package com.ysxsoft.wdg.ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.socialize.UMShareAPI;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.FriendAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.DiscussBean;
import com.ysxsoft.wdg.bean.home.InviteBean;
import com.ysxsoft.wdg.bean.response.ShareCodeResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;
import com.ysxsoft.wdg.widget.SharePopupWindow;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ShareNetActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_money_all)
    TextView tvMoneyAll;
    @BindView(R.id.tv_people_num)
    TextView tvPeopleNum;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;
    @BindView(R.id.ll_friends)
    LinearLayout llFriends;

    private FriendAdapter adapter;
    private int page = 1;
    private String uid = "";

    private SharePopupWindow popupWindow;
    private UMShareAPI mShareAPI;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_share_net;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this, true);
        initStatusBar(topView, true);
        titleContent.setText("分享邀请");

        mShareAPI = UMShareAPI.get(this);
        popupWindow = new SharePopupWindow(this, mShareAPI);
        uid = SharePrefUtils.getUserId();

        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setFocusable(false);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new FriendAdapter(R.layout.item_share_friends2);
        recyclerview.setAdapter(adapter);

        multStatusView.showLoading();
        onRefresh(refreshLayout);

    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);

    }

    @OnClick({R.id.title_finish, R.id.tv_rule, R.id.btn_invite})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv_rule:
                startActivity(new Intent(this, ShareRuleActivity.class));
                break;
            case R.id.btn_invite:
                getInviteCode();
                break;
        }
    }

    private void getInviteCode() {
        mApiHelper.getInviteCode(uid, new Observer<BaseBean<ShareCodeResponse>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<ShareCodeResponse> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    ShareCodeResponse data = baseBean.getData();
                    String code = data.getCode();
                    String url=data.getUrl();
                    popupWindow.setShareData(getString(R.string.app_name), "邀请码：" + code/*+data*/, url, "", 0);
                    popupWindow.show(mRootView);
                } else {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtils.showToast("请稍后再试");
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore(500);

        page++;
        getList();

    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh(500);

        page = 1;
        adapter.clear();
        getList();
    }

    private void getList() {
        mApiHelper.getInviteList(uid, page + "", new Observer<BaseBean<InviteBean>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<InviteBean> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    InviteBean data = baseBean.getData();
                    if (page == 1) {

                        llFriends.setVisibility(data.getList().size() > 0 ? View.VISIBLE : View.GONE);

                        tvPeopleNum.setText(data.getNum());
                        tvMoneyAll.setText(data.getMoney());
                        adapter.setNewData(data.getList());
                    } else {
                        adapter.addData(data.getList());
                    }
                } else {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                multStatusView.showContent();
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mShareAPI.onActivityResult(requestCode, resultCode, data);
    }

    class FriendAdapter extends BaseQuickAdapter<DiscussBean,BaseViewHolder> {


        public FriendAdapter(int layoutResId) {
            super(layoutResId);
        }

        public void clear(){
            getData().clear();
            notifyDataSetChanged();
        }


        @Override
        protected void convert(BaseViewHolder helper, DiscussBean item) {

            helper.setText(R.id.tv_nick,item.getNickname());
            helper.setText(R.id.tv_phone,item.getUsername());
            helper.setText(R.id.tv_num,item.getCount());
            helper.setText(R.id.tv_money,item.getMoney()+"元");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mShareAPI.release();
    }


}
